package com.shen.thread.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//通过反射获取泛型
public class TestFanxing {
    public static void main(String[] args) throws NoSuchMethodException {
        Class aClass = TestFanxing.class;
        Method method = aClass.getMethod("test1", Map.class, List.class);
        Type[] parameterTypes = method.getGenericParameterTypes();

        for (Type type : parameterTypes) {
            System.out.println(type);
            if (type instanceof ParameterizedType) {//instanceof  属于
                Type[] types = ((ParameterizedType) type).getActualTypeArguments();
                for (Type type1 : types) {
                    System.out.println(type1);
                }
            }
        }
        System.out.println("================");
        Method method2 = aClass.getMethod("test2", null);
        Type returnType = method2.getGenericReturnType();
        if (returnType instanceof ParameterizedType) {//instanceof  属于
            Type[] types = ((ParameterizedType) returnType).getActualTypeArguments();
            for (Type type1 : types) {
                System.out.println(type1);
            }
        }
    }


    public void test1(Map<String, User> map, List<User> list) {
        System.out.println("1");
    }

    public Map<String, User> test2() {
        System.out.println("2");
        return null;
    }
}
